<template>
    <div>
        <el-form :model="userinfo" ref="userinfo" :rules="rules" label-width="100px" class="demo-dynamic">
            <el-form-item label="用户名" prop="username">
                <el-input type="text" v-model="userinfo.username" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="userinfo.pass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="userinfo.checkPass" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
                <el-input v-model="userinfo.email"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('userinfo')">注册</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import axios from 'axios';
export default {
    data(){
         var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
            if (this.userinfo.checkPass !== '') {
                this.$refs.userinfo.validateField('checkPass');
            }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.userinfo.pass) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            userinfo: {
                email: '',   //邮箱
                pass:'',     //密码
                checkPass:'', //确认密码
                username:''  //用户名
            },
            rules: {
                username:[
                    {required: true, message: '请输入用户名', trigger: 'blur' }
                ],
                pass: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { required: true, message: '请输入确认密码', trigger: 'blur' },
                    { validator: validatePass2, trigger: 'blur' }
                ],
                email:[
                    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
                    { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
                ]
            }
        }
    },
    methods:{
        //提交表单  注册
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    let {username,email,pass} = this.userinfo;
                    axios.post('/api/registry',{username,email,password:pass}).then(res => {
                        console.log(res);
                        if(res.data.code === 1){
                            this.$message({
                                message: '恭喜你，注册成功',
                                type: 'success',
                                duration:1000,
                                onClose:()=>{
                                    this.$router.push('/login');
                                }
                            });
                        }else{
                            this.$message.error('注册失败');
                        }
                    })
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        }
    }
}
</script>